package cn.it.ygh.controller;

import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;

import cn.it.ygh.annotation.SystemLog;
import cn.it.ygh.pojo.User;
import cn.it.ygh.service.UserManageService;

@Controller(value="userManageController")
@RequestMapping("/userManage")
public class UserManageController {
	@Resource
	private UserManageService userManageService;
	
	@RequestMapping(value="/getUserById.do")
	@SystemLog(description="查看用户详细信息")
	public ModelAndView getUserById(@RequestParam(value="userId") int userId, HttpSession session){
		ModelAndView modelAndView = new ModelAndView();
		User user = userManageService.getUserById(userId);
		session.setAttribute("temp", user);
		modelAndView.setViewName("../admin/jsp/user-add");
		return modelAndView;
	}
	
	@RequestMapping("/getAllUser.do")
	public ModelAndView getAllUser(){
		ModelAndView modelAndView = new ModelAndView();
		List<User> userList = userManageService.getAllUser();
		modelAndView.addObject("userList", userList);
		modelAndView.setViewName("../admin/jsp/users-list");
		return modelAndView;
		
	}
	@RequestMapping("/deleteUser.do")
	@SystemLog(description="删除用户信息")
	public ModelAndView deleteUser(@RequestParam(value = "userId") Integer userId) throws DataIntegrityViolationException{
		ModelAndView modelAndView = new ModelAndView();
		System.out.println(userId);
		userManageService.deleteUser(userId);
		modelAndView.setViewName("../admin/jsp/users-list");
		return modelAndView;
	}
	
	@RequestMapping(value="/modifyUser.do",method=RequestMethod.POST)
	@SystemLog(description="修改用户信息")
	public ModelAndView modifyUser(User user){
		System.out.println("---");
		ModelAndView modelAndView = new ModelAndView();
		System.out.println(user.getUserAccount());
		userManageService.modifyUser(user);
		modelAndView.setViewName("../admin/jsp/users-list");
		return modelAndView;
	}
	
	@RequestMapping(value="/addUser.do",method=RequestMethod.POST)
	@SystemLog(description="添加用户")
	public ModelAndView addUser(User user){
		System.out.println("----");
		ModelAndView modelAndView = new ModelAndView();
		user.setUserTime(new Date());
		userManageService.addUser(user);
		modelAndView.setViewName("../admin/jsp/users-list");
		return modelAndView;
	}
	
	@RequestMapping(value="/batchdeleteUser.do")
	@SystemLog(description = "批量删除用户") 
	public ModelAndView batchdeleteUser(@RequestParam(value = "userIds") String userIds){
		ModelAndView modelAndView = new ModelAndView();
		String str[] = userIds.split("#");
		int[] ids = new int[str.length];
		for(int i = 0;i < str.length;i++)
			ids[i] = Integer.parseInt(str[i]);
		userManageService.batchdeleteUser(ids);
		modelAndView.setViewName("../admin/jsp/users-list");
		return modelAndView;
	}
	
	@RequestMapping(value="/findUser.do",method=RequestMethod.POST)
	public ModelAndView findUser(@RequestParam(value = "key") String key)
	{
		ModelAndView modelAndView = new ModelAndView();
		List<User> userList = userManageService.findUser(key);
		modelAndView.addObject("userList", userList);
		modelAndView.setViewName("../admin/jsp/users-list");
		return modelAndView;
	}
	
	@RequestMapping(value="/getPageData.do",method=RequestMethod.GET)
	@SystemLog(description = "获取分页数据") 
	@ResponseBody
	public Map<Object, Object> getPageData(@RequestParam(value = "curr") int curr,@RequestParam(value = "pageSize") int pageSize,@RequestParam(value = "key") String key)
	{
		List<User> userList = userManageService.getPageData((curr-1)*pageSize, pageSize,key);
		System.out.println(curr);
		System.out.println(pageSize);
		System.out.println(key);
		int totalRecord = userManageService.getTotalRecord(key);
		Map<Object, Object> info = new HashMap<Object, Object>();
		info.put("data", userList);
		info.put("total", totalRecord);
		JSONArray array = new JSONArray();
		array.addAll(userList);
		System.out.println(array.toString());
		return info;
	}
	
}
